<?php

namespace app\home\controller;

use app\home\controller\Common;
use think\Db;
use think\facade\Env;

class Cert extends Common
{
    function mobile()
    {
        $id = input('get.id');
        $info = db('cert')->where('id=' . $id)->find();
        $this->assign('info', $info);// 赋值数据集
        return view();
    }

    function getCert()
    {
        $cert_file = input('post.cert_file');
        $number = input('post.number');
        $json['errcode'] = 0;
        $json['message'] = '获取成功';
        $file_path = 'userfiles' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . $cert_file . DIRECTORY_SEPARATOR . $number . '.png';
        if (!file_exists($file_path)) {
            $json['errcode'] = 1;
            $json['message'] = '获取失败';
        }
        $https = $this->is_https();
        $host = $https ? 'https://' : 'http://' . $_SERVER['SERVER_NAME'];
        $json['file'] = $host . DIRECTORY_SEPARATOR . $file_path;
        echo json_encode($json);
        return;
    }

    function is_https()
    {
        if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
            return true;
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
            return true;
        } elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
            return true;
        } else {
            return false;
        }
    }
}
